package bot.controller;

import bot.mapper.UserMapper;
import bot.pojo.User;
import bot.pojo.UserExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.LocalDateTime;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Random;
@RestController
@RequestMapping("/user")
public class UesrController {
    UserExample userExample =new UserExample();
    @Autowired
    UserMapper userMapper;

    @PostMapping("/sign")
    public String sign(@RequestBody User user) throws ParseException {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        Random r = new Random();
        userExample.clear();
        //存在就更新不新增
        userExample.createCriteria().andQqIdEqualTo(user.getQqId()).andGroupIdEqualTo(user.getGroupId());
        List<User> users = userMapper.selectByExample(userExample);
        if(users.size() == 0){
            Calendar calendar1 = Calendar.getInstance();
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            calendar1.add(Calendar.DATE, 1);
            //一个群一人一天只能签到一次
            String three_days_ago = sdf1.format(calendar1.getTime());

            Integer i = r.nextInt(1000);
            user.setGoldCoin(i.toString());
            user.setSignIn(simpleDateFormat.format(new Date()));
            user.setEgxignTime(three_days_ago);
            userMapper.insert(user);
            return "签到成功,获得了"+i+"枚金币,当前剩余"+user.getGoldCoin()+"金币";
        }else{
            // 获取时间
            long longtime = getTime(users.get(0).getSignIn(),users.get(0).getEgxignTime());
            if(longtime > 0){
                return "今天已经签到拉，明天再来罢";
            }

            Calendar calendar1 = Calendar.getInstance();
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            calendar1.add(Calendar.DATE, 1);
            //一个群一人一天只能签到一次
            String three_days_ago = sdf1.format(calendar1.getTime());
            //存在时更新金币数
            Integer goldCoin =Integer.valueOf(users.get(0).getGoldCoin());
            Integer i = r.nextInt(1000);
            goldCoin = goldCoin + i;
            users.get(0).setGoldCoin(goldCoin.toString());
            //更新签到时间
            users.get(0).setSignIn(simpleDateFormat.format(new Date()));
            users.get(0).setEgxignTime(three_days_ago);
            userMapper.updateByPrimaryKey(users.get(0));
            return "签到成功,获得了"+i+"枚金币,当前剩余"+users.get(0).getGoldCoin()+"金币";
        }
    }
//    获取金币数
    @PostMapping("/getcoin")
    public String getcoin(@RequestBody User user){
        userExample.clear();
        //存在就更新不新增
        userExample.createCriteria().andQqIdEqualTo(user.getQqId()).andGroupIdEqualTo(user.getGroupId());
        List<User> users = userMapper.selectByExample(userExample);
        if(users.size() == 0){
            return "没有数据呐，建议去签到";
        }
        return "你有:"+users.get(0).getGoldCoin()+"金币,下次可签到时间为:"+users.get(0).getEgxignTime();
    }
    // 获取两个时间相差分钟数
    public static long getTime(String oldTime,String newTime) throws ParseException {

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        long NTime =df.parse(newTime).getTime();
        //从对象中拿到时间
        long OTime = df.parse(oldTime).getTime();
        long diff=(NTime-OTime)/1000/60;
        return diff;
    }
}
